﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WCFServer.BusinessEntities;
using WCFServer.DataAccess.Generic;
using System.Globalization;

namespace WCFServer.DataAccess
{
    /// <summary>
    /// A factory object that is responsible for taking a Klient
    /// and generating the corresponding SQL to insert that
    /// Klient into the database. It also includes a method
    /// to grab the returned ID from the call and updating the
    /// Klient object with it.
    /// </summary>
    internal class KlientInsertFactory : IDbToBusinessEntityNameMapper, IInsertFactory<Klient>
    {
        /// <summary>
        /// Creates the KlientInsertFactory to build an insert statement for
        /// the given Klient object.
        /// </summary>
        /// <param name="Klient">New Klient to insert into the database.</param>
        public KlientInsertFactory()
        {
        }

        #region IInsertFactory<Klient> Members

        public DbCommand ConstructInsertCommand(Database db, Klient klient)
        {
            DbCommand command = db.GetStoredProcCommand("dbo.AddKlient");

            if (klient.imie != null)
            {
                db.AddInParameter(command, "Imie", DbType.String, klient.imie);
            }
            if (klient.mail != null)
            {
                db.AddInParameter(command, "Mail", DbType.String, klient.mail);
            }
            if (klient.miasto != null)
            {
                db.AddInParameter(command, "Miasto", DbType.String, klient.miasto);
            }
            if (klient.nazwisko != null)
            {
                db.AddInParameter(command, "Nazwisko", DbType.String, klient.nazwisko);
            }
            if (klient.nick != null)
            {
                db.AddInParameter(command, "Nick", DbType.String, klient.nick);
            }
            if (klient.plec != null)
            {
                db.AddInParameter(command, "Plec", DbType.String, klient.plec);
            }
            return command;
        }

        public void SetNewID(Database db, DbCommand command, Klient klient)
        {
            //TODO: Provide set mapping
        }

        #endregion

        #region IDbToBusinessEntityNameMapper Members
        /// <summary>
        /// Maps a field name in the database (usually a parameter name for a stored proc)
        /// to the corresponding business entity property name.
        /// </summary>
        /// <remarks>This method is intended for error message handling, not for reflection.</remarks>
        /// <param name="dbParameter">Name of field/parameter that the database knows about.</param>
        /// <returns>Corresponding business entity field name.</returns>
        public string MapDbParameterToBusinessEntityProperty(string dbParameter)
        {
            switch (dbParameter)
            {
                case "Imie":
                    return "imie";
                case "Mail":
                    return "mail";
                case "Miasto":
                    return "miasto";
                case "Nazwisko":
                    return "nazwisko";
                case "Nick":
                    return "nick";
                case "Plec":
                    return "plec";
                default:
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter);
            }
        }
        #endregion
    }

}

